<?php
namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Session;

class Banner extends Base
{
    public function index()
    {
        $db = db('banner');
        $list = $db->where('status',1)->select();
        $this->assign(
            ['list' => $list]
        );
        return $this->fetch();
    }


    public function add(Request $request){
        $db = db('banner');
        if( $request->isPost() ){
            $data = input('post.');
            $data['img'] = upload();
            if( $db->save($data) )
            {
                $this->success('插入成功','index/index');
            }else
            {
                $this->error('插入失败','index/index');
            }
        }
        return $this->fetch();
    }


    public function edit(Request $request){
        $db = db('banner');
        if( $request->isPost() ){
            $data = input('post.');
            $data['img'] = upload();
            $res = $db->update($data);
            if( $res )
            {
                $this->success('修改成功','/admin/banner/index');
            }else
            {
                $this->error('修改失败','/admin/banner/index');
            }
        }elseif ( $request->isAjax() )
        {
            $status = $request->post('status');
            $id = $request->post('id');
            $res = $db->where('id',$id)->update('status',$status);
            if( $res )
            {
                echo 1;
            }else
            {
                echo 0;
            }
        }
        else{
            $id = $request->get('id');
            $vo = $db->find($id);
            $this->assign('vo',$vo);
            return $this->fetch();
        }
    }


    public function Del(Request $request){
        $db = db('Banner');
        if( $request->isAjax() )
        {
            $id = $request->post('id');
            $res = $db->delete($id);
            if( $res )
            {
                echo 1;
            }else
            {
                echo 0;
            }

        }
        else
        {
            $id = $request->get('id');
            $res = $db->delete($id);
            if( $res )
            {
                $this->success('删除成功','/admin/banner/index');
            }else
            {
                $this->error('删除失败','/admin/banner/index');
            }
        }
    }
}
